package pw.hais.etgsh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

import pw.hais.etgsh.R;
import pw.hais.etgsh.app.BaseActivity;
import pw.hais.etgsh.app.HaisApplication;
import pw.hais.etgsh.fragment.DianTaiFragment;
import pw.hais.etgsh.fragment.GuShiFragment;
import pw.hais.etgsh.fragment.HomeFragment;
import pw.hais.etgsh.fragment.SettingFragment;
import pw.hais.etgsh.service.CheckUpdateService;
import pw.hais.utils.L;

/**
 * Created by Hais1992 on 2015/12/25.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ImageView image_btn_home, image_btn_gushi, image_btn_diantai, image_btn_shezhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(context, CheckUpdateService.class));
        getSupportActionBar().hide();

        image_btn_home = (ImageView) findViewById(R.id.image_btn_home);
        image_btn_home.setOnClickListener(this);
        image_btn_gushi = (ImageView) findViewById(R.id.image_btn_gushi);
        image_btn_gushi.setOnClickListener(this);
        image_btn_diantai = (ImageView) findViewById(R.id.image_btn_diantai);
        image_btn_diantai.setOnClickListener(this);
        image_btn_shezhi = (ImageView) findViewById(R.id.image_btn_shezhi);
        image_btn_shezhi.setOnClickListener(this);

        image_btn_gushi.performClick();
    }


    private long exitTime = 0;

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            L.showShort("再次按返回键退出 " + getResources().getString(R.string.app_name));
            exitTime = System.currentTimeMillis();
        } else {
            ((HaisApplication)getApplication()).exitApp();
        }
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if(fragments!=null){
            for (Fragment f : fragments) {
                if(f!=null && f.isVisible())transaction.hide(f);
            }
        }

        Fragment fragment=null;
        switch (v.getId()) {
            case R.id.image_btn_home:
                fragment = getSupportFragmentManager().findFragmentByTag("Home");
                if(fragment==null){
                    fragment = new HomeFragment();
                    transaction.add(R.id.layout_main,fragment,"Home");
                }
                bottomBack(image_btn_home);
                break;
            case R.id.image_btn_gushi:
                fragment = getSupportFragmentManager().findFragmentByTag("GuShi");
                if(fragment==null){
                    fragment = new GuShiFragment();
                    transaction.add(R.id.layout_main,fragment,"GuShi");
                }
                bottomBack(image_btn_gushi);
                break;
            case R.id.image_btn_diantai:
                fragment = getSupportFragmentManager().findFragmentByTag("DianTai");
                if(fragment==null){
                    fragment = new DianTaiFragment();
                    transaction.add(R.id.layout_main,fragment,"DianTai");
                }
                bottomBack(image_btn_diantai);
                break;
            case R.id.image_btn_shezhi:
                fragment = getSupportFragmentManager().findFragmentByTag("Setting");
                if(fragment==null){
                    fragment = new SettingFragment();
                    transaction.add(R.id.layout_main,fragment,"Setting");
                }
                bottomBack(image_btn_shezhi);
                break;
        }

        transaction.show(fragment).commit();
    }

    private void bottomBack(View v){
        image_btn_home.setBackgroundColor(0x00000000);
        image_btn_gushi.setBackgroundColor(0x00000000);
        image_btn_diantai.setBackgroundColor(0x00000000);
        image_btn_shezhi.setBackgroundColor(0x00000000);
        v.setBackgroundColor(getResources().getColor(R.color.app_divider));
    }
}

